package com.dhcc.sds.base.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.sds.base.entity.SdsDictypeEntity;
import com.dhcc.sds.base.entity.SdsQcentityRuleEntity;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsDicService;
import com.dhcc.sds.base.service.ISdsDictypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.base.entity.SdsQcentityTipsEntity;
import com.dhcc.sds.base.service.ISdsQcentityTipsService;


/**
 * 诊疗内容提示
 * 
 * @ClassName: SdsQcentityTipsController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:47
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsqcentitytips")
public class SdsQcentityTipsController extends BaseController {

	private String PREFIX = "/backend/sds/sdsqcentitytips/";

	@Autowired
	private ISdsQcentityTipsService sdsQcentityTipsService;
	@Autowired
    private ISdsCommonService sdsCommonService;

    /**
     * 跳转到诊疗内容提示列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        // 查询页面下拉框数据
        Map<String, Object> comBMap = new HashMap<>();
        comBMap.put("TipsTime", sdsCommonService.getDicArray("QCTip"));
        comBMap.put("TipsRate", sdsCommonService.getDicArray("TipRate"));
        comBMap.put("TipsExp", sdsCommonService.findExps("QCTip"));//code与诊疗时机相同，纯属偶然
        model.addAttribute("combMap", comBMap);
        return PREFIX + "sdsqcentitytips.html";
    }

    @RequestMapping("/tipsinfo")
    @ResponseBody
    public Object tipsInfo(@RequestParam Map<String, Object> params){
        Wrapper<SdsQcentityTipsEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("entity_Id", params.get("entityId")).eq("tips_tim_dr", params.get("tipsTimDr"));
        return this.sdsQcentityTipsService.selectOne(wrapper);
    }
	
    /**
     * 跳转到新增诊疗内容提示
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsqcentitytips_add.html";
    }
	
	/**
     * 跳转到修改诊疗内容提示
     */
    @RequestMapping("/to_update/{sdsQcentityTipsId}")
    public String toUpdate(@PathVariable Long sdsQcentityTipsId, Model model) {
        SdsQcentityTipsEntity sdsQcentityTips = this.sdsQcentityTipsService.selectById(sdsQcentityTipsId);
        model.addAttribute("sdsQcentityTips",sdsQcentityTips);
        return PREFIX + "sdsqcentitytips_edit.html";
    }

    /**
     * 获取诊疗内容提示列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsQcentityTipsEntity> page = new PageFactory<SdsQcentityTipsEntity>().defaultPage();
        List<SdsQcentityTipsEntity> result = this.sdsQcentityTipsService.page(page,params);
        page.setRecords((List<SdsQcentityTipsEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增诊疗内容提示
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsQcentityTipsEntity sdsQcentityTips) {
        if (CommonUtil.isOneEmpty(sdsQcentityTips)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsQcentityTips.insert();
        return R.ok();
    }

    /**
     * 删除诊疗内容提示
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsQcentityTipsId) {
	
        this.sdsQcentityTipsService.deleteById(sdsQcentityTipsId);

        return R.ok();
    }

    /**
     * 编辑诊疗内容提示
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit(SdsQcentityTipsEntity sdsQcentityTips) {
        if (CommonUtil.isOneEmpty(sdsQcentityTips)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        Wrapper<SdsQcentityTipsEntity> wpr = new EntityWrapper<>();
        wpr.eq("entity_id", sdsQcentityTips.getEntityId()).eq("tips_tim_dr", sdsQcentityTips.getTipsTimDr());
        SdsQcentityTipsEntity old = this.sdsQcentityTipsService.selectOne(wpr);
        if (CommonUtil.isEmpty(old)) {
            sdsQcentityTips.insert();
        } else {
            old.setEntityId(sdsQcentityTips.getEntityId());
            old.setExpParam(sdsQcentityTips.getExpParam());
            old.setTipsContent(sdsQcentityTips.getTipsContent());
            old.setTipsDays(sdsQcentityTips.getTipsDays());
            old.setTipsRate(sdsQcentityTips.getTipsRate());
            old.setHandStop(sdsQcentityTips.getHandStop());
            old.setLinkExp(sdsQcentityTips.getLinkExp());
            old.setTipsTimDr(sdsQcentityTips.getTipsTimDr());
            old.updateAllColumnById();
        }
        return R.ok();
    }
}
